﻿using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class ShipUIShipInfoData: ShipUIData
{

    public override JsonData Save()
    {
        JsonData root = base.Save();
        return root;
    } 
    public override bool Load(JsonData root)
    {
        if (!base.Load(root))
            return false;
        return true;
    }
}
public class ShipUIShipInfo : ShipUI
{
    public enum OperationType
    {
        NONE,
        OprationTypeEnd
    }

    public Image m_MaskImage;
    public Image m_Image;
    bool m_HasStructChanged = false;


    public ShipUIShipInfo()
    {
        m_Type = ShipUIType.ShipInfo;
        m_ShipUIData = ShipUIManager.Instance.Create((int)m_Type);
    }

    private void Awake()
    {
        if(m_Image.sprite == null)
        {
            var texture = new Texture2D(256, 256);
            for (int i = 0; i < texture.width; ++i)
            {
                for (int j = 0; j < texture.height; ++j)
                {
                    texture.SetPixel(i, j, Color.clear);
                }
            }
            texture.Apply();
            m_Image.sprite = Sprite.Create(texture, new Rect(0, 0, 256, 256), new Vector2(0.5f, 0.5f));
        }
        
    }
    private void Update()
    {
        if(m_Ship)
            transform.rotation = m_Ship.transform.rotation;
    }
    private void FixedUpdate()
    {
        if (m_HasStructChanged)
        {
            m_HasStructChanged = false;
            m_Image.sprite.texture.Apply();
        }
    }
    int gradWidth, gradHeight;
    Vector2 hexSize;
    float hexRadius;
    Texture2D texture;
    public override void BindShip(Ship ship)
    {
        base.BindShip(ship);
        ship.onDamaged.AddListener(OnShipDamaged);
        gradWidth = m_Ship.structGrad.GetLength(0);
        gradHeight = m_Ship.structGrad.GetLength(1);
        texture = m_Image.sprite.texture;
        hexSize = Ship.GetGradSizeByGradVerticalNumber(texture.height, gradHeight);
        hexRadius = hexSize.y / 2f;


        m_MaskImage.sprite = ship.GetComponent<SpriteRenderer>().sprite;
        InitGrad();

    }

    public void OnShipDamaged(Vector2Int pos, int damageValue)
    {
        Debug.Log("Call ShipUI::OnShipDamaged:: "+pos);
        UpdateGrad(pos);
        m_HasStructChanged = true;
    }
    public void InitGrad()
    {

        for (int x=0; x<gradWidth; ++x)
        {
            for(int y=0; y<gradHeight; ++y)
            {
                var structValue = m_Ship.structGrad[x, y]; 
                if (structValue > 0)
                {
                    UpdateGrad(new Vector2Int(x, y));
                }
            }
        }
        texture.Apply();
    }

    public void UpdateGrad(Vector2Int pos)
    {
        HexGrid hexPos = new HexGrid(pos.x, pos.y);
        var pixCenterPos = hexPos.GetDescartesPosition(hexRadius);
        pixCenterPos.y = -pixCenterPos.y;

        var pixCenterPosI = new Vector2Int();
        pixCenterPosI.x = (int)Mathf.Clamp(pixCenterPos.x + (hexSize.x / 2), 0, texture.width);//     六边形应完全被图片包围，这样就能对其舰船的贴图了
        pixCenterPosI.y = (int)Mathf.Clamp(pixCenterPos.y + (hexSize.y / 2), 0, texture.height);

        List<Vector3> cornerPos = new List<Vector3>();
        for (int c = 0; c < 6; ++c)
        {
            var cornPos = hexPos.GetCornerPosition(c, hexRadius);
            cornPos.y = -cornPos.y;
            cornPos.x = Mathf.Clamp(cornPos.x + (hexSize.x / 2), 0, texture.width);//     六边形应完全被图片包围，这样就能对其舰船的贴图了
            cornPos.y = Mathf.Clamp(cornPos.y + (hexSize.y / 2), 0, texture.height);
            cornerPos.Add(cornPos);
            if (c > 0)
            {
                UtilsDraw.DrawLine(texture, cornerPos[c - 1], cornerPos[c], new Color(0.4f, 0.4f, 0.4f, 1));
            }
        }
        UtilsDraw.DrawLine(texture, cornerPos[5], cornerPos[0], new Color(0, 1, 0, 1));
        UtilsDraw.FullColor(texture, pixCenterPosI.x, pixCenterPosI.y, texture.GetPixel(pixCenterPosI.x, pixCenterPosI.y), new Color(0.2f, 0.8f, 0.5f, 0.7f * (m_Ship.structGrad[pos.x, pos.y] / 100f)));
    }
    public override string GetTypeName()
    {
        return "舰船信息";
    }
    public override void ApplyData()
    {
        base.ApplyData();
    }

    //public override string GetOperationName(int operation)
    //{
    //    string name = "None";
    //    switch((OperationType)operation)
    //    {
    //    }
    //    return name;
    //}
    //public override UnityEvent GetOperationEvent(int operationType)
    //{
    //    UnityEvent selectedEvent = null;
    //    switch ((OperationType)operationType)
    //    {
    //    }
    //    return selectedEvent;
    //}

}